package com.zhuchenhua.drugmanagementsystem.database;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import com.zhuchenhua.drugmanagementsystem.bean.Drug;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class DrugsDAO {

    private Context context;
    private MyDBHelper dbHelper;
    private SQLiteDatabase db;

    //构造函数
    public DrugsDAO(Context context) {
        this.context = context;
    }

    //打开数据库
    public void open() throws SQLiteException {
        dbHelper = new MyDBHelper(context);
        try {
            db = dbHelper.getWritableDatabase();
        } catch (SQLiteException ex) {
            db = dbHelper.getReadableDatabase();
        }
    }

    //关闭数据库
    public void close() {
        if (db != null) {
            db.close();
            db = null;
        }
    }


    //添加信息
    public long addDrugs(Drug drug) {
        // 创建ContentValues对象
        ContentValues values = new ContentValues();
        // 向该对象中插入键值对
        values.put("id", drug.id);
        values.put("name", drug.name);
        values.put("department", drug.department);
        values.put("drugNum", drug.drugNum);

        // 调用insert()方法将数据插入到数据库当中
        return db.insert("tb_Drugs", null, values);
    }

    //删除指定信息
    public int deletDrugs(Drug drug) {
        return db.delete("tb_Drugs", "id=?", new String[]{String.valueOf(drug.id)});
    }

    //修改指定信息
    public int updateDrugs(Drug drug) {
        ContentValues value = new ContentValues();
        value.put("name", drug.name);
        value.put("department", drug.department);
        value.put("drugNum", drug.drugNum);
        return db.update("tb_Drugs", value, "id=?", new String[]{String.valueOf(drug.id)});
    }

    //根据工号查找订单
    @SuppressLint("Range")
    public Drug getDrugs(String id) {
        //查询药品
        Cursor cursor = db.query("tb_Drugs", null, "id=?", new String[]{id}, null, null, null);
       Drug drug = new Drug();
        while (cursor.moveToNext()) {
            drug.id = (cursor.getString(cursor.getColumnIndex("id")));
            drug.name = (cursor.getString(cursor.getColumnIndex("name")));
            drug.department = (cursor.getString(cursor.getColumnIndex("department")));
            drug.drugNum = (cursor.getString(cursor.getColumnIndex("drugNum")));
        }
        return drug;
    }

    //查找所有取药信息
    @SuppressLint("Range")
    public ArrayList<Map<String, Object>> getAllDrugs() {
        ArrayList<Map<String, Object>> listDrugs = new ArrayList<>();
        try (Cursor cursor = db.query("tb_Drugs", null, null, null, null, null, null)) {
            int resultCounts = cursor.getCount();//获取总的数据项数
            if (resultCounts == 0) {
                return null;
            } else {
                while (cursor.moveToNext()) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", cursor.getString(cursor.getColumnIndex("id")));
                    map.put("name", cursor.getString(cursor.getColumnIndex("name")));
                    map.put("department", cursor.getString(cursor.getColumnIndex("department")));
                    map.put("drugNum", cursor.getString(cursor.getColumnIndex("drugNum")));

                    listDrugs.add(map);
                }
                return listDrugs;
            }
        }
    }
}